package com.neusoft.neuiotms.dcam.fixed.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.bean.TreeBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetTypeDTO;
import com.neusoft.neuiotms.dcam.fixed.service.AssetTypeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "固定资产类型管理")
@RestController
@RequestMapping(value = "/api/w/fixed/assetType")
public class AssetTypeController {

    @Autowired
    private AssetTypeService assetTypeService;

    @ApiOperation("查询所有固定资产类型")
    @GetMapping
    public ResultListDTO<AssetTypeDTO> index() {
        final List<AssetTypeDTO> assetTypeDTOs = this.assetTypeService.findAll();
        return ResultListDTO.success(assetTypeDTOs);
    }

    @ApiOperation("分页查询所有固定资产类型")
    @GetMapping("/p")
    public PageResultDTO<AssetTypeDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.assetTypeService.search(pageable, searchable));
    }

    @ApiOperation("创建固定资产类型")
    @PostMapping("")
    public ResultDTO<AssetTypeDTO> create(@RequestBody final AssetTypeDTO dto) {
        final AssetTypeDTO result = this.assetTypeService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "修改固定资产类型")
    @PutMapping("/{id}")
    public ResultDTO<AssetTypeDTO> update(@PathVariable final Long id,
            @RequestBody final AssetTypeDTO dto) {
        final AssetTypeDTO result = this.assetTypeService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("固定资产类型详情")
    @GetMapping("/{id}")
    public ResultDTO<AssetTypeDTO> details(@PathVariable final Long id) {
        final AssetTypeDTO dto = this.assetTypeService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除固定资产类型")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.assetTypeService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("资产类型树")
    @GetMapping("/tree")
    public ResultListDTO<TreeBean> tree() {
        return ResultListDTO.success(this.assetTypeService.tree());
    }
}
